<div class="layui-fluid  layui-layout-admin">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend class="title"></legend>
    </fieldset>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="title" autocomplete="off" placeholder="请输入名称" class="layui-input" value="">
            </div>
        </div>
		
		<div class="layui-form-item">
		    <label class="layui-form-label">标识</label>
		    <div class="layui-input-inline">
		        <input type="text" name="name" autocomplete="off" placeholder="请输入名称" class="layui-input" value="">
		    </div>
		</div>

        <div class="layui-form-item">
            <label class="layui-form-label">接口地址</label>
            <div class="layui-input-inline">
                <input type="text" name="path" placeholder="请输入接口地址" autocomplete="off" class="layui-input" value="">
            </div>
        </div>
		
		<div class="layui-form-item">
		    <label class="layui-form-label">模板地址</label>
		    <div class="layui-input-inline">
		        <input type="text" name="file_path" placeholder="请输入模板地址" autocomplete="off" class="layui-input" value="">
		    </div>
		</div>

        <div class="layui-form-item">
            <label class="layui-form-label">icon</label>
            <div class="layui-input-inline">
                <input type="text" name="icon" placeholder="请输入菜单icon" autocomplete="off" class="layui-input" value="">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">导航显示</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" lay-skin="switch" lay-filter="isNav" lay-text="ON|OFF">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">上级权限</label>
            <div class="layui-input-inline">
                <select name="pid">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
        	<div class="layui-input-block">
        		<div class="layui-footer">
					<input type="hidden" name="id" value="0" />
					<input type="hidden" name="is_nav" value="1" />
					<input type="hidden" name="list_table_name" value="auth-list" />
					<button class="layui-btn" lay-submit lay-filter="LAY-send-submit">立即提交</button>
				</div>
			</div>
        </div>
    </form>
</div>
<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
</script>

<script >
	layui.data.sendParams = function(params){
		layui.use('form', function () {
			let form = layui.form, $ = layui.$;

			$(".layui-form").attr('action', layui.setter.API_HOST + 'admin/authority/save')
			let _auth_id = params.id | 0;
			$(".title").text((_auth_id > 0 ? '编辑' : '添加') + '权限');

			if (_auth_id) {
				let _index = layer.load();
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/authority/save',
					data: {
						id: _auth_id
					},
					done: function(res) {
						layer.close(_index);
						if (res.code == 0) {
							$("input[name='title']").val(res.data.title);
							$("input[name='name']").val(res.data.name);
							$("input[name='path']").val(res.data.path);
							$("input[name='file_path']").val(res.data.file_path);
							$("input[name='icon']").val(res.data.icon);
							$("input[name='is_nav']").val(res.data.is_nav);
							$("input[name='addon']").val(res.data.addon);
							$("input[name='id']").val(res.data.id);
							get_auth_tree(res.data.pid)
						} else {
							layer.msg('请求失败', {icon: 5})
						}
					}
				});
			} else {
				get_auth_tree(0);
			}

			//监听指定开关
			form.on('switch(isNav)', function(data){
				$("input[name='is_nav']").val(this.checked ? 1 : 0);
			});
			
			function get_auth_tree(_data_pid)
			{
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/authority/auth_tree',
					done: function(res) {
						if (res.code == 0) {
							let _html = [];
							_html.push('<option value="0">顶级权限</option>');
							$.each(res.data, function(i, d) {
								if (_data_pid == d.id) {
									_html.push('<option value="'+ d.id +'" selected>'+ d.html +' '+ d.title +'</option>');
								} else {
									_html.push('<option value="'+ d.id +'">'+ d.html +' '+ d.title +'</option>');
								}
							});
							$("select[name='pid']").html(_html.join(''));
							form.render();
						}
					}
				});
			}
		});
	}
</script>